<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="js/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/iconfont.css">
    <link rel="stylesheet" href="css/main.css">
    <link type="text/css" rel="stylesheet" href="js/jedate/css/jedate.css">
    <script src="js/jquery-1.12.4.min.js"></script>
    <script src="js/jedate/js/jedate.js"></script>
    <script src="js/tinymce/js/tinymce/tinymce.min.js"></script>
    <script src="js/tinymce/js/tinymce_setup.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章编辑
        </div>
        <div class="container-fluid common_con">
            <form class="form-horizontal article_form">
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">文章标题：</label>
                    <div class="col-sm-10">
                        <input type="email" name="title" class="form-control title" id="inputEmail3" value="文章标题文字">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputPassword3" class="col-sm-2 control-label">文章封面：</label>
                    <div class="col-sm-10">
                        <img src="images/pic06.jpg" class="article_cover">
                        <input name="cover" type="file" id="exampleInputFile">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">文章类别：</label>
                    <div class="col-sm-4">
                        <select class="form-control category" name="categoryId">
                            <option>类别一</option>
                            <option>类别二</option>
                            <option>类别三</option>
                            <option selected>类别四</option>
                            <option>类别五</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">发布时间：</label>
                    <div class="col-sm-4">

                        <div class="input-group">
                            <input type="text" name="date" class="form-control" value="2019-05-17" id="dateinput">
                            <span class="input-group-btn">
                                <button class="btn btn-default" type="button"
                                    onclick="jeDate('#dateinput',{trigger:false,format: 'YYYY-MM-DD'})"><i
                                        class="iconfont icon-icondate"></i></button>
                            </span>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">文章内容：</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="rich_content" placeholder="文章内容" value="文章内容">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-success btn-edit">修改</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <!-- 引入一个模板引擎js文件 -->
    <script src="./js/template-web.js"></script>
    <!-- 准备一个 文章分类 模板 -->
    <script id="cateList" type="text/html">
        {{each data v}}
        <option value='{{v.id}}'>{{v.name}}</option>
        {{/each}}
    </script>
    <script>
        // 入口函数
        $(function () {
            // 需求: 一进到文章编辑页面,显示所有的文章类别到下拉菜单.
            //1.一进到页面发送ajax获取所有的文章分类.
            $.ajax({
                type: 'get',
                url: 'http://localhost:8080/api/v1/admin/category/list',
                success: function (backData) {
                    //console.log(backData);
                    if (backData.code == 200) {
                        //2.通过模板引擎渲染到筛选按钮 左边的下拉菜单中.
                        var resHtml = template('cateList', backData);
                        $('select.category').html(resHtml);
                    }
                }
            });


            // 需求: 获取传递过来的文章id. window.location.search ?和?后面的东西
            var id = window.location.search.split("=")[1];
            // 需求: 根据这个文章id,发送ajax请求,获取该文章的详细信息.
            $.ajax({
                type: 'get',
                url: 'http://localhost:8080/api/v1/admin/article/search',
                data: {
                    id: id
                },
                success: function (backData) {
                    if (backData.code == 200) {
                        //获取成功,把对应的数据显示在对应的标签上.
                        $('input.title').val(backData.data.title); //文章标题
                        $('.article_cover').attr('src', backData.data.cover); //文章封面
                        $('select.category').val(backData.data.categoryId); //文章类别
                        $('#dateinput').val(backData.data.date); //时间.
                        $('#rich_content').val(backData.data.content); //内容
                        //tinyMCE.activeEditor.setContent(backData.data.content)
                    }
                }
            });


            //需求: 点击修改按钮,完成修改.
            //1.给修改按钮注册点击事件.
            $('.btn-edit').on('click', function (e) {
                e.preventDefault();
                //2.创建fd对象
                var fd = new FormData($('form')[0]);
                //追加一个修改后的内容
                fd.append('content', tinyMCE.activeEditor.getContent());
                //追加文章id到fd中去.
                fd.append('id',id);
                //3.发送ajax请求,完成修改
                $.ajax({
                    type:'post',
                    url:'http://localhost:8080/api/v1/admin/article/edit',
                    data:fd,
                    contentType:false,
                    processData:false,
                    success:function(backData){
                        if(backData.code == 200){
                            alert('修改成功!');
                        }
                    }
                });
            });
            //4.提示修改成功就ok


        });
    </script>
</body>

</html>